package com.dom4j;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.xml.crypto.AlgorithmMethod;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.dl.DL;
import com.test.Test;
import com.zc.ZC;
//读写用户密码.xml信息
public class YM extends SJH{
	static Document document = null;
	public static Element root = null;
	static {
		SAXReader reader = new SAXReader();
		try {
			document = reader.read("用户密码.xml");
			root = document.getRootElement();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
//保存信息
	private static void sava() {
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter writer = null;
		try {
			writer = new XMLWriter(new FileWriter("用户密码.xml"), format);
			writer.write(document);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	//删除信息
public  static void sc(String str){
	Iterator it =root.elementIterator();
	while(it.hasNext()){
		Element element=(Element)it.next();
		if(element.attributeValue("num").equals(str)){
			element.getParent().remove(element);
		}
	}
	sava();
}
//添加信息
	public static void add(String str, String name, String pwd, String num,
			int tc) {
		Element element = root.addElement("手机号");
		element.addAttribute("num", str);
		Element celement = element.addElement("密码");
		celement.addAttribute("password", pwd);
		Element ccElement1 = celement.addElement("用户名");
		ccElement1.addAttribute("name", name);
		Element ccElement2 = celement.addElement("余额");
		ccElement2.addAttribute("num", num);
		switch (tc) {
		case 1:
			add1(celement, ZF.hlElement.attributeValue("name"), ZF.hlElement);
			break;
		case 2:
			add1(celement, ZF.wcElement.attributeValue("name"), ZF.wcElement);
			break;
		case 3:
			add1(celement, ZF.crElement.attributeValue("name"), ZF.crElement);
			break;
		}
		sava();
	}
//辅助添加信息的方法
	static void add1(Element celement, String name, Element element) {
		Element ccElement3 = celement.addElement(name);
		Element cccElement1 = ccElement3.addElement("通话时间");
		cccElement1.addText(element.elementText("通话时间"));
		Element cccElement2 = ccElement3.addElement("上网流量");
		cccElement2.addText(element.elementText("上网流量"));
		Element cccElement3 = ccElement3.addElement("短信条数");
		cccElement3.addText(element.elementText("短信条数"));

	}
//比较输入的手机号和密码是否和XML里面的一致
	public static boolean bj(String num, String pwd1) {
		Iterator it = root.elementIterator();
		while (it.hasNext()) {
			Element aElement = (Element) it.next();
			if (num.equals(aElement.attributeValue("num"))) {
				Element cElement = aElement.element("密码");
				if (pwd1.equals(cElement.attributeValue("password"))) {
					return true;
				}
			} 
		}
			System.out.println("手机号或密码输入错误");
			Test.zjm();
		return false;
	}
//充值之后的钱，重新写入XML里
	public static void ye(int num) {
		Iterator it = root.elementIterator();
		while (it.hasNext()) {
			Element aElement = (Element) it.next();
			if (DL.num.equals(aElement.attributeValue("num"))) {
				Element cElement = aElement.element("密码").element("余额");
				String a = cElement.attributeValue("num");
				int b = Integer.valueOf(a);
				String d = String.valueOf((num + b));
				cElement.attribute("num").setValue(d);
				sava();
			}

		}

	}
//消费之后套餐余量。写入xml里
	public static void xf(long num, String name, String name1) {
		Iterator it = root.elementIterator();
		while (it.hasNext()) {
			Element aElement = (Element) it.next();
			if (DL.num.equals(aElement.attributeValue("num"))) {
				List<Element> list=aElement.element("密码").elements();
				Element cElement =null;
				String a=null;
				long b=0;
				for(int i=0;i<list.size();i++){
					cElement = list.get(i).element(name);
					if(cElement!=null){
						a = cElement.getText();
						b = Long.valueOf(a);
						break;
					}
				}
				if (b > 0) {
					long c = b - num;
					String d = String.valueOf((c));
					if (c >= 0) {
						System.out.println("剩余" + d + name1);
					} else {
						System.out.println("余额不足,已欠" + d + name1);
					}
					cElement.setText(d);
				} else {
					Element cElement1 = aElement.element("密码").element("余额");
					String a1 = cElement1.attributeValue("num");
					long b1 = Long.valueOf(a1);
					long c = b1 - num;
					String d = String.valueOf((c));
					if (c >= 0) {
						System.out.println("剩余" + d + "元");
					} else {
						System.out.println("余额不足,已欠费" + d);
					}	
					cElement1.attribute("num").setValue(d);
				}JE.xf(num,name,name1,b);sava();break;
			}
		}
	}

}
